package com.berchina.config;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.jetty.JettyServerCustomizer;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Slf4j
@Configuration
@PropertySource("classpath:/config.properties")
public class JettyConfig {

    @Value("${port}")
    private int port;

//    @Bean
//    public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory(
//            JettyServerCustomizer jettyServerCustomizer) {
//        JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory();
//        factory.addServerCustomizers(jettyServerCustomizer);
//        factory.setPort(port);
//        log.info("初始化jetty完成，监听端口：" + port);
//        return factory;
//    }

//SpringBoot2.X写法
    @Bean
    public ServletWebServerFactory servletContainer( JettyServerCustomizer jettyServerCustomizer) {
        JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
        factory.addServerCustomizers(jettyServerCustomizer);
        factory.setPort(port);
        log.info("初始化jetty完成，监听端口：" + port);
        return factory;
    }

//    @Bean
//    public ServletWebServerFactory servletContainer() {
//        JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
//                factory.setPort(port);
//        log.info("初始化jetty完成，监听端口：" + port);
//        return factory;
//    }

    @Bean
    public JettyServerCustomizer jettyServerCustomizer() {
        return server -> {
            final QueuedThreadPool threadPool = server.getBean(QueuedThreadPool.class);
            threadPool.setMaxThreads(100);
            threadPool.setMinThreads(20);
        };
    }


}
